﻿using Aop.Api.Domain;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using WCS.Infrastructure.Attr;
using WCS.Infrastructure.Dto.Request.Epaylinks;
using WCS.Infrastructure.Dto.Request.Organization;
using WCS.Infrastructure.EFCore;
using WCS.Service.Interface;

namespace WCS.Api.Admin.Controllers
{
    /// <summary>
    /// 组织架构
    /// </summary>
    [ApiController]
    public class OrganizationController : BaseController
    {
        private readonly IConfiguration _configuration;
        private readonly IOrganizationService _organizationService;
        public OrganizationController(IConfiguration configuration, IOrganizationService organizationService, IUserService userService) : base(userService)
        {
            _configuration = configuration;
            _organizationService = organizationService;
        }

        /// <summary>
        /// 新增系统角色【RoleName，Level，OrganizationType，Description】
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        [ActionLog(Action = "新增角色", Group = "组织架构")]
        public async Task<IActionResult> AddRole(RoleRequestDto dto)
        {
            var result = await _organizationService.AddRoleAsync(dto);
            return Ok(result);
        }

        /// <summary>
        /// 删除系统角色【ids[]】
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        [ActionLog(Action = "删除角色", Group = "组织架构")]
        public async Task<IActionResult> DeleteRole(RoleRequestDto dto)
        {
            var result = await _organizationService.DeleteRoleAsync(dto);
            return Ok(result);
        }

        /// <summary>
        /// 编辑系统角色【RoleName，Level，OrganizationType，Description】
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        [ActionLog(Action = "编辑角色", Group = "组织架构")]
        public async Task<IActionResult> UpdateRole(RoleRequestDto dto)
        {
            var result = await _organizationService.UpdateRoleAsync(dto);
            return Ok(result);
        }

        /// <summary>
        /// 获取系统角色列表【page,limit,RoleName,OrgTypeList】
        /// </summary>
        [HttpPost]
        public async Task<IActionResult> GetRoleList(RoleRequestDto dto)
        {
            var result = await _organizationService.GetRoleListAsync(dto);
            return Ok(result);
        }

        /// <summary>
        /// 获取用户角色商户列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetUserRoleMerchantList(string? userId)
        {
            var result = await _organizationService.GetUserRoleMerchantListAsync(userId);
            return Ok(result);
        }

        /// <summary>
        /// 注册商户【Name，ContactPerson，ContactPhone】
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        [ActionLog(Action = "注册商户", Group = "商户管理")]
        [AllowAnonymous]
        public async Task<IActionResult> RegisteredMerchant(MerchantRequestDto dto)
        {
            var result = await _organizationService.RegisteredMerchantAsync(dto);
            return Ok(result);
        }

        /// <summary>
        /// 商户进件(调用易票联接口)【MerId,MerchantName,Paper,AcceptOrder,Business】
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        [ActionLog(Action = "商户进件", Group = "商户管理")]
        public async Task<IActionResult> MerchantEntry(EpaylinksMerchantInfoRequestDto dto)
        {
            var result = await _organizationService.MerchantEntryAsync(dto);
            return Ok(result);
        }

        /// <summary>
        /// 商户补件(调用易票联接口)【MerId，MerchantName，Paper】
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        [ActionLog(Action = "商户补件", Group = "商户管理")]
        public async Task<IActionResult> MerchantSupplement(EpaylinksMerchantInfoRequestDto dto)
        {
            var result = await _organizationService.MerchantSupplementAsync(dto);
            return Ok(result);
        }

        /// <summary>
        /// 新增结算账户
        /// </summary>
        /// <param name="dto"></param>
        /// <param name="merId"></param>
        /// <returns></returns>
        [HttpPost]
        [ActionLog(Action = "商户新增结算账户", Group = "商户管理")]
        public async Task<IActionResult> AddSettlementAccount(EpaylinksSettlementAccountInfo dto, string merId)
        {
            var result = await _organizationService.AddSettlementAccountAsync(dto, merId);
            return Ok(result);
        }

        /// <summary>
        /// 图片/附件上传(调用易票联接口)
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        [ActionLog(Action = "图片/附件上传", Group = "文件管理")]
        [AllowAnonymous]
        public async Task<IActionResult> FileUpload(CreateUpdateFileUploadDto dto)
        {
            var result = await _organizationService.FileUploadAsync(dto);
            return Ok(result);
        }

        /// <summary>
        /// 商户审核结果回调通知(调用易票联接口)
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        [ActionLog(Action = "商户审核结果回调通知", Group = "商户管理")]
        [AllowAnonymous]
        public async Task<IActionResult> HandleMerchantAuditResultNotification(MerchantAuditResultNotificationRequest dto)
        {
            var result = await _organizationService.HandleMerchantAuditResultNotificationAsync(dto);
            return Ok(result);
        }

        /// <summary>
        /// 查询进件商户信息(调用易票联接口)（并更新状态）【MerId】
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        [ActionLog(Action = "查询进件商户信息（并更新状态）", Group = "商户管理")]
        public async Task<IActionResult> QueryMerchant(QueryMerchantInfoRequestDto dto)
        {
            var result = await _organizationService.QueryMerchantAsync(dto);
            return Ok(result);
        }

        /// <summary>
        /// 获取用户商户列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetUserMerchantList()
        {
            var result = await _organizationService.GetUserMerchantListAsync();
            return Ok(result);
        }

        /// <summary>
        /// 获取商户列表(平台后端接口)
        /// </summary>
        /// <param name="page">页数</param>
        /// <param name="limit">每页条数</param>
        /// <param name="key">查询条件【根据商户名称】</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetMerchantList(int page = 1, int limit = 10, string? key = "")
        {
            var result = await _organizationService.GetMerchantListAsync(new MerchantRequestDto
            {
                Page = page,
                Limit = limit,
                Key = key
            });
            return Ok(result);
        }

        /// <summary>
        /// 获取单个商户详情
        /// </summary>
        /// <param name="id">商户ID</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetMerchantDetail(string id)
        {
            var result = await _organizationService.GetMerchantDetailAsync(id);
            return Ok(result);
        }

        /// <summary>
        /// 获取单个商户进件信息详情
        /// </summary>
        /// <param name="id">商户ID</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetMerchantEntryDetail(string id)
        {
            var result = await _organizationService.GetMerchantEntryDetailAsync(id);
            return Ok(result);
        }

        /// <summary>
        /// 编辑商户信息【Name，MerchantType，Address，DetailedAddress，Location，ContactPerson，ContactPhone】
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        [ActionLog(Action = "编辑商户", Group = "商户管理")]
        public async Task<IActionResult> UpdateMerchant(MerchantRequestDto dto)
        {
            var result = await _organizationService.UpdateMerchantAsync(dto);
            return Ok(result);
        }

        /// <summary>
        /// 新增门店【Name，Address，DetailedAddress，StoreType，MerchantId，ContactPerson，ContactPhone】
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        [ActionLog(Action = "新增门店", Group = "门店管理")]
        public async Task<IActionResult> AddStore(StoreRequestDto dto)
        {
            var result = await _organizationService.AddStoreAsync(dto);
            return Ok(result);
        }

        /// <summary>
        /// 编辑门店【Id，Name，Address，DetailedAddress，StoreType，ContactPerson，ContactPhone，SettlementCycle，EnableStatus】
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>

        [HttpPost]
        [ActionLog(Action = "编辑门店", Group = "门店管理")]
        public async Task<IActionResult> UpdateStore(StoreRequestDto dto)
        {
            var result = await _organizationService.UpdateStoreAsync(dto);
            return Ok(result);
        }

        /// <summary>
        /// 获取门店列表
        /// </summary>
        /// <param name="page">页数</param>
        /// <param name="limit">每页条数</param>
        /// <param name="key">查询条件【根据门店名称】</param>
        /// <param name="merchantId">商户ID</param>
        [HttpGet]
        public async Task<IActionResult> GetStoreList(int page = 1, int limit = 10, string? key = "", string? merchantId = "")
        {
            var result = await _organizationService.GetStoreListAsync(new StoreRequestDto
            {
                Page = page,
                Limit = limit,
                Key = key,
                MerchantId = merchantId,
            });
            return Ok(result);
        }

        /// <summary>
        /// 获取单个门店详情
        /// </summary>
        /// <param name="Id">门店ID</param>
        /// <returns></returns>
        [HttpGet]
        [AllowAnonymous]
        public async Task<IActionResult> GetStoreDetail(string? Id = "")
        {
            var result = await _organizationService.GetStoreDetailAsync(new StoreRequestDto
            {
                Id = Id
            });
            return Ok(result);
        }

        /// <summary>
        /// 在门店详情里批量设置关联成员
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        [ActionLog(Action = "在门店详情里批量设置关联成员", Group = "团队管理")]
        public async Task<IActionResult> SetStoreMembers(BindMembersRequestDto dto)
        {
            var result = await _organizationService.SetStoreMembersAsync(dto);
            return Ok(result);
        }

        /// <summary>
        /// 更新登录用户的角色选择（用户切换商户角色）
        /// </summary>
        /// <param name="userRoleId">团队成员角色ID</param>
        /// <returns></returns>
        [HttpGet]
        [ActionLog(Action = "更新登录用户的角色选择（用户切换商户角色）", Group = "团队管理")]
        public async Task<IActionResult> UpdateLoginUserRole(string userRoleId)
        {
            var result = await _organizationService.UpdateLoginUserRoleAsync(userRoleId);
            return Ok(result);
        }

        /// <summary>
        /// 检查当前手机号的用户是否在该商户下存在角色
        /// </summary>
        /// <param name="merchantId">商户ID</param>
        /// <param name="phone">手机号</param>
        /// <returns></returns>
        [HttpGet]
        [AllowAnonymous]
        public async Task<IActionResult> CheckTeamMembers(string? merchantId = "", string? phone = "")
        {
            var result = await _organizationService.CheckTeamMembersAsync(new UserRoleRequestDto
            {
                MerchantId = merchantId,
                Phone = phone
            });
            return Ok(result);
        }

        /// <summary>
        /// 检查手机号用户是否存在
        /// </summary>
        /// <param name="phone"></param>
        /// <returns></returns>
        [HttpGet]
        [AllowAnonymous]
        public async Task<IActionResult> CheckUser(string phone)
        {
            var result = await _organizationService.CheckUserAsync(phone);
            return Ok(result);
        }

        /// <summary>
        /// 邀请新增团队成员【Phone，RoleId，MerchantId，StoreIds，CodeIds，Name】
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        [ActionLog(Action = "邀请新增团队成员", Group = "团队管理")]
        [AllowAnonymous]
        public async Task<IActionResult> AddTeamMembers(UserRoleRequestDto dto)
        {
            var result = await _organizationService.AddTeamMembersAsync(dto);
            return Ok(result);
        }

        /// <summary>
        /// 团队成员确认加入【Id(团队成员ID)，Name，Phone，Code(验证码)】
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        [ActionLog(Action = "团队成员确认加入", Group = "团队管理")]
        [AllowAnonymous]
        public async Task<IActionResult> TramMembersConfirm(UserRoleRequestDto dto)
        {
            var result = await _organizationService.TramMembersConfirmAsync(dto);
            return Ok(result);
        }

        /// <summary>
        /// 获取团队成员列表
        /// </summary>
        /// <param name="page">页数</param>
        /// <param name="limit">每页条数</param>
        /// <param name="key">查询条件【根据成员昵称】</param>
        /// <param name="merchantId">商户ID</param>
        /// <param name="storeId">门店ID</param>
        /// <param name="roleType">角色类型</param>
        [HttpGet]
        public async Task<IActionResult> GetTeamMembersList(int page = 1, int limit = 10, string? key = "", string? merchantId = "", string? storeId = "", int? roleType = -1)
        {
            var result = await _organizationService.GetTeamMembersListAsync(new UserRoleRequestDto
            {
                Page = page,
                Limit = limit,
                Key = key,
                MerchantId = merchantId,
                StoreId = storeId,
                RoleType = roleType
            });
            return Ok(result);
        }

        /// <summary>
        /// 获取单个团队成员详细信息
        /// </summary>
        /// <param name="id">团队成员ID</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetTeamMembers(string? id = "")
        {
            var result = await _organizationService.GetTeamMembersAsync(new UserRoleRequestDto
            {
                Id = id
            });
            return Ok(result);
        }

        /// <summary>
        /// 编辑团队成员信息【Id，Name，RoleId，StoreIds/StoreId，CodeIds/CodeId】
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> UpdateTeamMembers(UserRoleRequestDto dto)
        {
            var result = await _organizationService.UpdateTeamMembersAsync(dto);
            return Ok(result);
        }

        /// <summary>
        /// 删除团队成员
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        [ActionLog(Action = "删除团队成员", Group = "团队管理")]
        public async Task<IActionResult> DeleteTeamMembers(DeleteUserRoleRequestDto dto)
        {
            var result = await _organizationService.DeleteTeamMembersAsync(dto);
            return Ok(result);
        }
    }
}
